package com.gyms.manager;

import android.content.Context;
import android.os.Environment;

import com.classic.okhttp.beans.HVCitiesBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyms.bean.LocationBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import constant.FileConstant;
import utils.FileUtils;
import utils.StringUtils;

import static constant.FileConstant.FILE_SELCT_CITY;

/**
 * ltx on 2016/12/27 17:16
 * 定位相关的逻辑
 */
public class LocationManger {
    private static LocationManger instance=null;
    public ArrayList<HVCitiesBean> mHVCitiesBeens;//所有城市的beans
    public HVCitiesBean mCurHVCitiesBean;//当前的城市的bean
    public String mCityCode="330100";//城市代号
    public Double longitude=null;//经度
    public Double latitude=null;//纬度
    //当前定位的bean
    public LocationBean mLocationBean;
    public String mLocationCity;//当前定位城市
    public String mCurCityName="杭州";//当前城市
    private boolean isLocationSuc;//定位是否成功
    private static File file,mCurrCity;
    private static Gson mGson;

    private String mDetaiAddress;//当前的详细地址
    private String mAdCode;//区号

    private LocationManger(Context mContext) {
        instance = this;
    }
    public static LocationManger getInstance(Context mContext) {
        if (instance == null) {
            instance = new LocationManger(mContext);
            mCurrCity = new File(Environment.getExternalStorageDirectory(), FileConstant.FILE_SELCT_CITY);//当前选择的城市存储的file
            file = new File(Environment.getExternalStorageDirectory(), FileConstant.FILE_CITY);
            mGson=new Gson();
        }
        return instance;
    }

    /**
     * 设置全部城市列表
     * @param mHVCitiesBeens 所有城市list
     */
    public void setHVCitiesBeens(ArrayList<HVCitiesBean> mHVCitiesBeens){
        this.mHVCitiesBeens=mHVCitiesBeens;
    }

    /**
     * 获取当前的城市Bean
     * @return 当前的城市bean
     */
    public HVCitiesBean getCurHVCitiesBean(){
        if(null==mCurHVCitiesBean){//如果当前的bean为null 获取默认为杭州
            getLocalCurrCity();//获取本地的当前城市bean
            if(null==mCurHVCitiesBean){
                    if(StringUtils.isNull(mHVCitiesBeens)){
                        getLocalCitys();
                    }
                for (HVCitiesBean mHVCitiesBean : mHVCitiesBeens) {
                    if (mHVCitiesBean.getMatchkey().equals("杭州市")) {//默认杭州
                        mCurHVCitiesBean = mHVCitiesBean;
                    }
                }
            }
        }
        return mCurHVCitiesBean;
    }
    /**
     * 获取本地的当前城市bean
     */

    private void getLocalCurrCity() {
        File mCurrCity = new File(Environment.getExternalStorageDirectory(), FILE_SELCT_CITY);//当前选择的城市存储的file
        String strCurrCity = FileUtils.readFile2String(mCurrCity, FileConstant.CHARSET_NAME);
        Gson mGson=new Gson();
        mCurHVCitiesBean = mGson.fromJson(strCurrCity, HVCitiesBean.class);

    }

    public void setCurCityBean(HVCitiesBean mHVCitiesBean){
        mCurHVCitiesBean=mHVCitiesBean;
    }

    /**
     * 根据cityName 来查询
     * @param cityName 城市名称
     */
    public void setCurCityBean(String cityName){
        if(StringUtils.isNull(mHVCitiesBeens)){
            getLocalCitys();
        }
        for(HVCitiesBean mHVCitiesBean:mHVCitiesBeens){
            String mTempName=mHVCitiesBean.getMatchkey();
            if(cityName.equals(mTempName)){//匹配当前的城市名称
                mCurHVCitiesBean=mHVCitiesBean;
                FileUtils.writeFileFromString(mCurrCity, mGson.toJson(mHVCitiesBean), false);//将默认数据写入文件
            }
        }
    }

    /**
     * 设置经度
     * @param longitude
     */
    public void setLongitude(Double longitude){
        this.longitude=longitude;
    }

    /**
     * 设置维度
     * @param latitude
     */
    public void setLatitude(Double latitude){
        this.latitude=latitude;
    }

    /**
     * 获取当前城市
     */
    public String getLocationCity(){
        mLocationCity= StringUtils.isNull(mLocationBean)?"定位失败":mLocationBean.getCity();//获取当前城市
        return mLocationCity;
    }

    /**
     * 定位是否成功
     * @return true 成功 false 失败
     */
    public boolean isLocationSuc() {
        return !StringUtils.isNull(mLocationBean);
    }
    /**
     * 点击定位城市设置当前的cityBean
     * 如果定位失败 默认为杭州
     * 否则根据当前的cityName来查询Bean
     */
    public void setLocationCityBean(){
        if(isLocationSuc()){//定位是否成功
            setCurCityBean(getLocationCity());//根据城市名设置当前的
        }else{
            setCurCityBean("杭州市");
        }
    }
    /**
     * 获取cityCode
     * 如果当前的城市的
     */
    public String getCityCode(){
        mCurHVCitiesBean=getCurHVCitiesBean();
        return StringUtils.isNull(mCurHVCitiesBean)?mCityCode:mCurHVCitiesBean.getAreaid();
    }
    /**
     * 获取当前的城市
     */
    public String getCurCityName(){
        mCurHVCitiesBean=getCurHVCitiesBean();
        mCurCityName=StringUtils.isNull(mCurHVCitiesBean)?mCurCityName:mCurHVCitiesBean.getName();
        return mCurCityName;
    }
    private void getLocalCitys() {
        String strCityList = FileUtils.readFile2String(file, FileConstant.CHARSET_NAME);
        mHVCitiesBeens = mGson.fromJson(strCityList, new TypeToken<List<HVCitiesBean>>() {
        }.getType());
        mHVCitiesBeens=mHVCitiesBeens==null?new ArrayList<HVCitiesBean>():mHVCitiesBeens;
    }

    public String getDetaiAddress() {
        return mDetaiAddress;
    }

    public void setDetaiAddress(String detaiAddress) {
        mDetaiAddress = detaiAddress;
    }

    public String getAdCode() {
        return mAdCode;
    }

    public void setAdCode(String adCode) {
        mAdCode = adCode;
    }
}
